% 3个数字的加减乘运算
clear;
% QuestionList
Question     = struct("numLim",[999,999,999],"resultLim",[999,999],"strfunc",[],"resultfunc",[],"Title","");
QuestionList = [];
% 增加一个题目模板
Question.Title      ="a+b+c";
Question.numLim     = [999,999,999];
Question.resultLim  = [999,999];
Question.strfunc    = @(a,b,c) sprintf('%2d+%2d+%2d=',a,b,c);
Question.resultfunc = @(a,b,c) [a+b, a+b+c];
QuestionList = [QuestionList;Question];

Question.Title      ="a+(b-c)";
Question.numLim     = [999,999,999];
Question.resultLim  = [999,999];
Question.strfunc    = @(a,b,c) sprintf('%2d+(%2d-%2d)=',a,b,c);
Question.resultfunc = @(a,b,c) [b-c, a+b-c];
QuestionList = [QuestionList;Question];

Question.Title      ="a+bxc";
Question.numLim     = [999,99,9];
Question.resultLim  = [999,999];
Question.strfunc    = @(a,b,c) sprintf('%2d+%2d x %2d=',a,b,c);
Question.resultfunc = @(a,b,c) [b*c, a+b*c];
QuestionList = [QuestionList;Question];

Question.Title      ="a-bxc";
Question.numLim     = [999,99,9];
Question.resultLim  = [999,999];
Question.strfunc    = @(a,b,c) sprintf('%2d-%2d x %2d=',a,b,c);
Question.resultfunc = @(a,b,c) [b*c, a-b*c];
QuestionList = [QuestionList;Question];

Question.Title      ="a*b/b+c";
Question.numLim     = [99,9,999];
Question.resultLim  = [999,999];
Question.strfunc    = @(a,b,c) sprintf(['%2d ',char(247),' %2d+%2d='],a*b,b,c);
Question.resultfunc = @(a,b,c) [a*b/b, a*b/b+c];
QuestionList = [QuestionList;Question];

Question.Title      ="a*b/b*c";
Question.numLim     = [99,9,9];
Question.resultLim  = [999,999];
Question.strfunc    = @(a,b,c) sprintf(['%2d ',char(247),' %2d x %2d='],a*b,b,c);
Question.resultfunc = @(a,b,c) [a*b, a*b/b*c];
QuestionList = [QuestionList;Question];

Question.Title      ="a+b*c/c";
Question.numLim     = [99,9,9];
Question.resultLim  = [999,999];
Question.strfunc    = @(a,b,c) sprintf(['%2d + %2d ',char(247),' %2d ='],a, b*c ,c);
Question.resultfunc = @(a,b,c) [b, a+b*c/c];
QuestionList = [QuestionList;Question];

Question.Title      ="(a+bc-a)/c";
Question.numLim     = [99,9,9];
Question.resultLim  = [99,9];
Question.strfunc    = @(a,b,c) sprintf(['(%2d + %2d)',char(247),' %2d ='],a,b*c-a,c);
Question.resultfunc = @(a,b,c) [b*c-a, b];
QuestionList = [QuestionList;Question];

Question.Title      ="b(a+c)c*(a/(a+c))";
Question.numLim     = [9,99,9];
Question.resultLim  = [9,999];
Question.strfunc    = @(a,b,c) sprintf(['M%2d{times}frac{%d}{%d}','='],b*(a+c),a,a+c);
Question.resultfunc = @(a,b,c) [a+c, b*(a+c)];
QuestionList = [QuestionList;Question];
% 页数
pagenum    = 20;
% 每页题数
numperpage = 20;
% 总题目数
maxcnt     = numperpage*pagenum;
% 问题列表
problemstr = cell(maxcnt, 1);
% 生成题目
for i = 1:maxcnt
    op            = randi([1,length(QuestionList)]);
    problemstr{i} = GenOneQuestion(QuestionList(op), false);
end
% 生成文档
GenDocument(problemstr, "mydoc", pagenum);

function str = GenOneQuestion(Question, withAnswer)
    while true
        num1 = randi(Question.numLim(1));
        num2 = randi(Question.numLim(2));
        num3 = randi(Question.numLim(3));
        result = Question.resultfunc(num1, num2, num3);
        if (result(1) < Question.resultLim(1) && result(1) >= 0) && ...
                (result(2) < Question.resultLim(2) && result(2) >= 0)
            str = Question.strfunc(num1, num2, num3);
            if withAnswer   
                str = [str, num2str(result(2))];%#ok
            end
            return;
        end
    end
end
function GenDocument(problemstr, filename, pagenum)
% 参数
% problemstr 题目列表
% filename 文件名
% pagenum 页数
import mlreportgen.dom.*
import mlreportgen.report.*
d = Document(filename,"docx");
open(d);
cnt   = 1;
for i = 1:pagenum
    % 表格式
    tableStyles       = { ColSep("solid"), RowSep("solid"), Border("solid"), Width("100%"), RowHeight('120pt','atleast'), FontSize("12pt")};
    % 表头格式
    tableHeaderStyles = { BackgroundColor("lightgray"), Bold(true), FontSize("16pt"),RowHeight('20pt')};
    % 表头名称
    headerLabels      = ["姓名：___","开始： ____","结束： ____" ,"得分： ____"];
    % 表数据 5行4列
    tableData = reshape(problemstr( cnt:(cnt+19),1),5,4);
    for idxi = 1:size(tableData,1)
        for idxj = 1:size(tableData,2)
            str = tableData{idxi, idxj};
            disp(str)
            if str(1) == 'M'
                strlatex = str(2:end);
                strlatex = strrep(strlatex, "frac","\frac");
                strlatex = strrep(strlatex, "times","\times");
                rpt = Report("mydoc","docx");
                eq = Equation(strlatex);
                eq.DisplayInline = true;
                img = Image(getSnapshotImage(eq,rpt));
                [~,fb] = fileparts(img.Path);
                copyfile(img.Path,[fb,'.svg']);
                tableData{idxi,idxj} = Image([fb,'.svg']);
            end
        end
    end
    cnt       = cnt + 20;
    cellTbl   = FormalTable(headerLabels,tableData);
    cellTbl.Style = [cellTbl.Style, tableStyles];
    cellTbl.Header.Style = [cellTbl.Header.Style, tableHeaderStyles];
    cellTbl.TableEntriesInnerMargin = "2pt";
    append(d,cellTbl);
    if i ~= pagenum
        br = PageBreak();
        append(d,br);
    end
end
close(d);
end